package models

import (
	"time"
	
	"gorm.io/gorm"
)

type User struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Username  string    `gorm:"uniqueIndex;size:50;not null" json:"username"`
	Email     string    `gorm:"uniqueIndex;size:100;not null" json:"email"`
	Password  string    `gorm:"not null" json:"-"`
	Role      string    `gorm:"size:20;default:'user'" json:"role"` // user, admin
	Avatar    string    `gorm:"size:255" json:"avatar"`
	Bio       string    `gorm:"type:text" json:"bio"`
	Website   string    `gorm:"size:255" json:"website"`
	Posts     []Post    `gorm:"foreignKey:UserID" json:"posts,omitempty"`
	Comments  []Comment `gorm:"foreignKey:UserID" json:"comments,omitempty"`
}

// 钩子函数 - 创建前生成时间戳
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
	u.CreatedAt = time.Now()
	u.UpdatedAt = time.Now()
	return
}

// 钩子函数 - 更新前更新时间戳
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) {
	u.UpdatedAt = time.Now()
	return
}